package com.gz.furnitures.web;

import com.gz.furnitures.entity.Admin;
import com.gz.furnitures.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//管理登录网址http://localhost:8080/furniture/views/manage/manage_login.jsp
//账号:admin
//密码:12345678
@WebServlet(urlPatterns = "/manage/adminServlet")
public class AdminServlet extends BasicServlet{
    AdminServiceImpl adminService = new AdminServiceImpl();
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Admin admin = adminService.login(new Admin(null, username, password, null));
        if(admin != null) {
//            System.out.println("登陆成功");
            request.getSession().setAttribute("admin", admin);
            request.getRequestDispatcher("/views/manage/manage_menu.jsp").forward(request,response);
        }else {
            //验证失败将用户名和错误信息返回
            request.setAttribute("msg","用户名或者密码错误");
            request.setAttribute("username",username);
            //返回登录页面
            request.getRequestDispatcher("/views/manage/manage_login.jsp").forward(request,response);
//            System.out.println("登录失败");
        }

    }
}
